<?php

namespace App\Console\Commands;

use App\Models\FlowParkRecord;
use Illuminate\Console\Command;
use Carbon\Carbon;
use App\Models\ParkInfo;
use App\Models\ParkRecord;
use App\Models\Report;

class ChargeCountDay extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'charge:count_day';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '收入统计';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $start = Carbon::yesterday()->timestamp;

        $end = Carbon::today()->timestamp;
        $m = 5;
        ini_set('memory_limit', '250M');
        for($i=30; $i<=30;$i++) {
            if ($i < 10) {
                $start = strtotime("2019-0$m-0" . $i . " 00:00:00");
                if (($i + 1) < 10) {
                    $end = strtotime("2019-0$m-0" . ($i + 1) . " 00:00:00");
                } else {
                    $end = strtotime("2019-0$m-" . ($i + 1) . " 00:00:00");
                }
            } else {
                $start = strtotime("2019-0$m-" . $i . " 00:00:00");
                if (($i + 1) <= 30) {
                    $end = strtotime("2019-0$m-" . ($i + 1) . " 00:00:00");
                } else {
                    $end = strtotime("2019-05-01 00:00:00");
                }
            }

            $parks = ParkInfo::whereIn('park_code', config('custom.report_parks'))
                ->select(['park_code', 'parking_name'])
                ->orderBy('sort_by', 'desc')
                ->get();


            $data = $parks->map(function ($item, $key) use ($start, $end) {
                //生成报表列
                $rowKey = 'time_' . $item->park_code;
                $row = [
                    $rowKey . '_0_15_little' => 0,
                    $rowKey . '_15_30_little' => 0,
                    $rowKey . '_0_15_big' => 0,
                    $rowKey . '_15_30_big' => 0,
                    $rowKey . '_little' => 0,
                    $rowKey . '_big' => 0,
                    $rowKey . '_total' => 0,
                    $rowKey . '_greater_1440_little' => 0,
                    $rowKey . '_greater_1440_big' => 0
                ];

                for ($i = 30; $i < 1440; $i += 30) {
                    $endTIme = $i + 30;
                    $row[$rowKey . "_{$i}_{$endTIme}_little"] = 0;
                    $row[$rowKey . "_{$i}_{$endTIme}_big"] = 0;
                }


                //查询前一天数据
                $table = 't_park_record_' . $item->park_code;
                $parkRecord = new ParkRecord();
                $parkRecord->setTable($table);
                $res = $parkRecord
                    ->where('out_time', '>=', $start)
                    ->where('out_time', '<', $end)
                    ->select(['parking_duration', 'parking_charge', 'vpl_color', 'charge_items'])->get();

                foreach ($res as $value) {
                    //定位时间范围
                    $filed = $this->initFiled($value, $rowKey);
                    $calc_charge = 0;
                    if ($value->charge_items != '[]' && $value->charge_items != '') {
                        $chargeItems = \GuzzleHttp\json_decode($value->charge_items, true);
                        foreach ($chargeItems as $v) {
                            if (!in_array($v['charge_type'], [1, 2, 4, 48, 49, 3, 50, 61])) continue;
                            if ($v['shishou'] > 0) {
                                $calc_charge += $v['shishou'];
                            }
                        }
                    }
//                $calc_charge = $value->parking_charge / 100;
                    $calc_charge = $calc_charge / 100;
                    //大车或者小车
                    if ($value->vpl_color == 1) {
                        $row[$filed . '_big'] += $calc_charge;
                        $row[$rowKey . '_big'] += $calc_charge;
                    } else {
                        $row[$rowKey . '_little'] += $calc_charge;
                        $row[$filed . '_little'] += $calc_charge;
                    }

                    $row[$rowKey . '_total'] += $calc_charge;
                }
                return $row;
            });

            //存入数据库
            $report = new Report();
            $report->report_date = date("Y-m-d",$start); //Carbon::yesterday()->format('Y-m-d');
            $report->type = 'charge_count_day_report';
            $report->value = $data->collapse();
            $report->save();
        }
        // 清除流量数据
//        FlowParkRecord::truncate();
        $this->info('收入统计成功');
    }

    /**
     * 计算时间范围
     *
     * @param $value
     * @param $key
     * @return string
     */
    private function initFiled($value, $key)
    {
        $time = ceil($value->parking_duration / 60);
        if ($time > 0 && $time <= 15) return "{$key}_0_15";
        if ($time > 15 && $time <= 30) return "{$key}_15_30";

        for ($i = 30; $i < 1440; $i += 30) {
            if ($time > $i && $time <= $i + 30) return "{$key}_" . $i . "_" . ($i + 30);
        }

        return $key . "_greater_1440";
    }
}

